package com.yonyou.gmcc.gateway.wechat.common;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {
	
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
	
	/**
	 * get请求，
	 * 参数为url可以在url后直接拼接参数
	 * @param reqUrl
	 * @return
	 */
	public static String sendGetRequest(String reqUrl) {
		long responseLength = 0;       //响应长度
		String responseContent = null; //响应内容
		HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
		HttpGet httpGet = new HttpGet(reqUrl);           //创建org.apache.http.client.methods.HttpGet
		try{
			HttpResponse response = httpClient.execute(httpGet); //执行GET请求
			HttpEntity entity = response.getEntity();            //获取响应实体
			if(null != entity){
				responseLength = entity.getContentLength();
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity); //Consume response content
			}
			logger.warn("请求地址: " + httpGet.getURI());
			logger.warn("响应状态: " + response.getStatusLine());
			logger.warn("响应长度: " + responseLength);
			logger.warn("响应内容: " + responseContent);
		}catch(ClientProtocolException e){
			logger.error("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下", e);
		}catch(ParseException e){
			logger.error(e.getMessage(), e);
		}catch(IOException e){
			logger.error("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下", e);
		}finally{
			httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源
		}
		return responseContent;
	}
	
	/**
	 * get请求，
	 * 参数为url和需要发送的参数,多个参数需要转&拼接的
	 * @param reqUrl
	 * @param sendData
	 * @return
	 */
	public static String sendGetRequest(String reqUrl, String sendData) {
		long responseLength = 0;       //响应长度
		String responseContent = null; //响应内容
		HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
		try{
			reqUrl += "?";
			reqUrl += sendData;
			
			HttpGet httpGet = new HttpGet(reqUrl);           //创建org.apache.http.client.methods.HttpGet
			HttpResponse response = httpClient.execute(httpGet); //执行GET请求
			HttpEntity entity = response.getEntity();            //获取响应实体
			if(null != entity){
				responseLength = entity.getContentLength();
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity); //Consume response content
			}
			logger.warn("请求地址: " + httpGet.getURI());
			logger.warn("响应状态: " + response.getStatusLine());
			logger.warn("响应长度: " + responseLength);
			logger.warn("响应内容: " + responseContent);
		}catch(ClientProtocolException e){
			logger.error("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下", e);
		}catch(ParseException e){
			logger.error(e.getMessage(), e);
		}catch(IOException e){
			logger.error("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下", e);
		}finally{
			httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源
		}
		return responseContent;
	}
	
	/**
	 * get请求
	 * 参数为url
	 * map参数
	 * @param reqUrl
	 * @param paramMap
	 * @return
	 */
	public static String sendGetRequest(String reqUrl, Map<String, Object> paramMap) {
		long responseLength = 0;       //响应长度
		String responseContent = null; //响应内容
		HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
		try{
			StringBuilder sendData = new StringBuilder();
			for(Entry<String, Object> entry : paramMap.entrySet()){
				sendData.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
			}
			if(sendData.length() > 0){
				sendData.setLength(sendData.length() - 1); //删除最后一个&符号
			}
			reqUrl += "?";
			reqUrl += sendData;
			
			HttpGet httpGet = new HttpGet(reqUrl);           //创建org.apache.http.client.methods.HttpGet
			HttpResponse response = httpClient.execute(httpGet); //执行GET请求
			HttpEntity entity = response.getEntity();            //获取响应实体
			if(null != entity){
				responseLength = entity.getContentLength();
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity); //Consume response content
			}
			logger.warn("请求地址: " + httpGet.getURI());
			logger.warn("响应状态: " + response.getStatusLine());
			logger.warn("响应长度: " + responseLength);
			logger.warn("响应内容: " + responseContent);
		}catch(ClientProtocolException e){
			logger.error("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下", e);
		}catch(ParseException e){
			logger.error(e.getMessage(), e);
		}catch(IOException e){
			logger.error("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下", e);
		}finally{
			httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源
		}
		return responseContent;
	}
	
	/**
	 * post请求
	 * @param reqUrl
	 * @return
	 */
	public static String sendPostRequest(String reqUrl) {
		long responseLength = 0;       //响应长度
		String responseContent = null;
		HttpClient httpClient = new DefaultHttpClient();
		
		HttpPost httpPost = new HttpPost(reqUrl);
		httpPost.setHeader(HTTP.CONTENT_TYPE, "text/html;charset=UTF-8");
		try{
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				responseLength = entity.getContentLength();
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity);
			}
			logger.warn("请求地址: " + httpPost.getURI());
			logger.warn("响应状态: " + response.getStatusLine());
			logger.warn("响应长度: " + responseLength);
			logger.warn("响应内容: " + responseContent);
		}catch(Exception e){
			logger.error("与[" + reqUrl + "]通信过程中发生异常,堆栈信息如下", e);
		}finally{
			httpClient.getConnectionManager().shutdown();
		}
		return responseContent;
	}
	
	/**
	 * post请求
	 * 参数为url和str参数
	 * @param reqUrl
	 * @return
	 */
	public static String sendPostRequest(String reqUrl, String sendData) {
		long responseLength = 0;       //响应长度
		String responseContent = null;
		HttpClient httpClient = new DefaultHttpClient();
		
		HttpPost httpPost = new HttpPost(reqUrl);
		httpPost.setHeader(HTTP.CONTENT_TYPE, "text/html;charset=UTF-8");
		try{
			httpPost.setEntity(new StringEntity(sendData, "UTF-8"));
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				responseLength = entity.getContentLength();
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity);
			}
			logger.warn("请求地址: " + httpPost.getURI());
			logger.warn("响应状态: " + response.getStatusLine());
			logger.warn("响应长度: " + responseLength);
			logger.warn("响应内容: " + responseContent);
		}catch(Exception e){
			logger.error("与[" + reqUrl + "]通信过程中发生异常,堆栈信息如下", e);
		}finally{
			httpClient.getConnectionManager().shutdown();
		}
		return responseContent;
	}
	
	/**
	 * post请求
	 * 参数为url和map参数
	 * @param reqUrl
	 * @return
	 */
	public static String sendPostRequest(String reqUrl, Map<String, Object> paramMap) {
		long responseLength = 0;       //响应长度
		String responseContent = null;
		HttpClient httpClient = new DefaultHttpClient();
		
		HttpPost httpPost = new HttpPost(reqUrl);
		httpPost.setHeader(HTTP.CONTENT_TYPE, "text/html;charset=UTF-8");
		try{
			StringBuilder sendData = new StringBuilder();
			for(Entry<String, Object> entry : paramMap.entrySet()){
				sendData.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
			}
			if(sendData.length() > 0){
				sendData.setLength(sendData.length() - 1); //删除最后一个&符号
			}
			reqUrl += "?";
			reqUrl += sendData;
			
			httpPost.setEntity(new StringEntity(sendData.toString(), "UTF-8"));
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				responseLength = entity.getContentLength();
				responseContent = EntityUtils.toString(entity, "UTF-8");
				EntityUtils.consume(entity);
			}
			logger.warn("请求地址: " + httpPost.getURI());
			logger.warn("响应状态: " + response.getStatusLine());
			logger.warn("响应长度: " + responseLength);
			logger.warn("响应内容: " + responseContent);
		}catch(Exception e){
			logger.error("与[" + reqUrl + "]通信过程中发生异常,堆栈信息如下", e);
		}finally{
			httpClient.getConnectionManager().shutdown();
		}
		return responseContent;
	}
}
